package cn.imvc.app.controllers.seller;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.controllers.goods.GoodsControllerImpl;
import cn.imvc.app.entities.Seller;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.SellerModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class SellerControllerImpl implements SellerController{
	static Logger logger = Logger.getLogger(SellerControllerImpl.class);
	public Page<Seller> page;
	public ViewRequest viewRequest;
	public Seller seller;
	public int id;
	public int[] ids;
	public String search_content;
	
	@Autowired
	SellerModel sellerModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		
		return null;
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		page = sellerModel.find(viewRequest);
		if(page!=null){
		return "seller/index";
		}else{
			return null;
		}
	}

	public String show() {
		return null;
	}

	public String editNew() {
		return "seller/editNew";
	}

	public String create() {
		int result = sellerModel.save(seller);
		if(result==0){
			return Render.json(new DialogBean("300","商家编码不能重复","seller","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","商家名称不能重复","seller","closeCurrent"));
		}
		loggingModel.info(logger,"添加商家："+seller.getName());
		return Render.json(new DialogBean("200","添加成功","","closeCurrent"));
	}

	public String edit() {
		seller = sellerModel.get(id);
		return "seller/edit";
	}

	public String update() {
		int result =  sellerModel.update(id,seller);
		if(result==0){
			return Render.json(new DialogBean("300","商家编码不能重复","seller","closeCurrent"));
		}
		if(result==-1){
			return Render.json(new DialogBean("300","商家名称不能重复","seller","closeCurrent"));
		}
		loggingModel.info(logger,"修改商家："+seller.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		String result = sellerModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除商家："+id);
			return Render.json(new DialogBean("200","删除成功","seller"));
		}else{
			return Render.json(new DialogBean("300", result,"seller")); 
		}
		
	}

	public String destroyBatch() {
		sellerModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","seller"));
	}
/**
 * 外部调用
 * @return
 */
    public String dialog() {
		
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		if(search_content!=null&&!search_content.equals("")){
			viewRequest.getParams().put("code_like", search_content);
			viewRequest.getParams().put("_or_name_like", search_content);
			viewRequest.getParams().put("_or_contacts_like", search_content);
			viewRequest.getParams().put("_or_telPhone_like", search_content);
			viewRequest.getParams().put("_or_mobPhone_like", search_content);
			viewRequest.getParams().put("_or_address_like", search_content);
		}
		page = sellerModel.find(viewRequest);
		return "seller/dialog";
	}
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public Seller getSeller() {
		return seller;
	}

	public void setSeller(Seller seller) {
		this.seller = seller;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public String getSearch_content() {
		return search_content;
	}

	public void setSearch_content(String search_content) {
		this.search_content = search_content;
	}

	
}
